﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibrarySync2.Common.Extensions
{
    public static class TypeExtensionMethods
    {
        public static TInstantiated Instantiate<TInstantiated>(this Type toInstantiate)
            where TInstantiated : class
        {
            return toInstantiate.Instantiate<TInstantiated>(new Type[] { }, new object[] { });
        }

        public static TInstantiated Instantiate<TInstantiated>(this Type toInstantiate, Type[] parameterTypes, object[] parameterValues)
            where TInstantiated : class
        {
            if (parameterTypes == null)
                throw new ArgumentNullException("parameterTypes");
            if (parameterValues == null)
                throw new ArgumentNullException("parameterValues");
            if (parameterTypes.Length != parameterValues.Length)
                throw new ArgumentException("parameterTypes and parameterValues do not have the same number of items.");

            var constructorInfo = toInstantiate.GetConstructor(parameterTypes);
            return constructorInfo.Invoke(parameterValues) as TInstantiated;
        }
    }
}
